package com.web.util;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Properties;


import config.props.*;
import com.mysql.jdbc.Statement; 

public class DBConnection {
	private static String url;
	private static String user;
	private static String password;
	static{	
       try {
    	   ClassLoader classLoader=DBConnection.class.getClassLoader();
    	   InputStream is=classLoader.getResourceAsStream("config/props/db.properties");
    	   Properties props=new Properties();
    	   props.load(is);
    	   url=props.getProperty("url");
    	   user=props.getProperty("user");
    	   password=props.getProperty("password");
           Class.forName(props.getProperty("driver"));                        
       } catch (ClassNotFoundException e) {
           e.printStackTrace();
           throw new RuntimeException();
       } catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		throw new RuntimeException();
	   }   
	}
    
       public static Connection getConnection() throws Exception{    	  
               return  DriverManager.getConnection(url,user,password);                         
    }
	
	public static void close(ResultSet rt,PreparedStatement prep,Connection conn)throws  Exception{
 	   if(rt!=null){
 		   rt.close();
 	   }
 	   if(prep!=null){
 		   prep.close();
 	   }
 	   if(conn!=null){
 		   conn.close();
 	   }
	}
	public static void close(PreparedStatement prep,Connection conn)throws  Exception{
	 	   
	 	   if(prep!=null){
	 		   prep.close();
	 	   }
	 	   if(conn!=null){
	 		   conn.close();
	 	   }
		}
}
